package com.hq.ims.platform.controller;

import com.hq.authority.utils.AppUtils;
import com.hq.ims.data.entityenum.EnumMenuOwnType;
import com.hq.ims.data.service.BasePermissionMenuService;
import com.hq.ims.data.service.BaseUserService;
import com.hq.ims.data.utils.LocalConstant;
import com.hq.ims.data.utils.WebExpandUtils;
import com.hq.ims.data.vo.PermissionMenuMapstruct;
import com.hq.ims.data.vo.UserMapstruct;
import com.hq.ims.platform.busservice.PlatformMyInfoService;
import com.hq.utils.vo.ReturnVo;
import com.hq.web.annotations.GetJsonValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统管理中心Controller
 */
@Api(tags = "平台用户心中")
@RestController
@RequestMapping(LocalConstant.SYS_TYPE_PLATFORM_URL_PREFIX + "/my")
public class PlatformMyInfoController {

    @Resource
    private BaseUserService baseUserService;
    @Resource
    private PlatformMyInfoService platformMyInfoService;
    @Resource
    private BasePermissionMenuService basePermissionMenuService;

    @ApiOperation(value = "获取我的权限", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/getMyAuthAuth", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<List<PermissionMenuMapstruct.PermissionMenuVo>> getMyAuthAuth(
    ) {
        String userId = AppUtils.getUserId();
        String companyId = WebExpandUtils.getHeadCompanyId();

        List<PermissionMenuMapstruct.PermissionMenuVo> permissionMenuVoList = basePermissionMenuService.getMenuByUserIdAndCompanyId(userId, companyId, EnumMenuOwnType.Platform);
        return ReturnVo.buildSuccess(permissionMenuVoList);
    }

    @ApiOperation(value = "获取我的个人信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/getMyInfo", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<UserMapstruct.UserAndCompanySimpleVo> getMyInfo(
    ) {
        UserMapstruct.UserAndCompanySimpleVo myInfo = platformMyInfoService.getMyInfo();
        return ReturnVo.buildSuccess(myInfo);
    }

    @ApiOperation(value = "变更密码", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/changePwd", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<Boolean> getPageByCompanyId(
            @GetJsonValue(required = false) UserMapstruct.ForgotPasswordVo saveVo,
            @GetJsonValue(zh = "公司ID") String smsCode
    ) {
        baseUserService.updateUserPwdValidSmsCode(smsCode, saveVo);
        return ReturnVo.buildSuccess(true);
    }

    @ApiOperation(value = "更新我的信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/updateMyInfo", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<Boolean> updateMyInfo(
            @RequestBody UserMapstruct.ChangeUserInfoVo userVo
    ) {
        baseUserService.updateUserInfo(userVo);
        return ReturnVo.buildSuccess(true);
    }

}
